<?php
//error_reporting(0);
function paraseString($dkpString) {
    $data       = GetXMLTree($dkpString);
    $raidData   = array();
    $index      = 0;
	$raidData['bossArray']['all']		= $GLOBALS['lang']['note43'];
    //按格式分析
    if($data['raidinfo']['playerinfos']['player']) {        //ctrt+ml1.5
        $raidData['start']  = $data['raidinfo']['start'];   //int
        $raidData['end']  = $data['raidinfo']['end'];       //int
        $raidData['zone']  = $data['raidinfo']['zone'];
        $totalTime      = $raidData['end']-$raidData['start'];
        $totalTime      = $totalTime?$totalTime:1;

        foreach($data['raidinfo']['playerinfos']['player'] as $key=>$val) {
            $playerName = trim($val['name']);
            $raidData['player'][$playerName]['race'] = ($val['race']);
            $raidData['player'][$playerName]['class'] = ($val['class']);
            $raidData['player'][$playerName]['level'] = ($val['level']);
            $raidData['player'][$playerName]['index'] = $index++;
        }
        foreach($data['raidinfo']['joins']['join'] as $key=>$val) {
            $playerName = trim($val['player']);
            $raidData['player'][$playerName]['join']    = $val['time'];
    	}
        foreach($data['raidinfo']['leaves']['leave'] as $key=>$val) {
            $playerName = trim($val['player']);
            $raidData['player'][$playerName]['leave']    = $val['time'];
    	}
        foreach($raidData['player'] as $key=>$val) {
			$playerName		= $key;
        	$raidData['player'][$playerName]['raidtime1'] = round(($raidData['player'][$playerName]['join'] - $raidData['start'])/$totalTime*200);
            $raidData['player'][$playerName]['raidtime2'] = round(($raidData['player'][$playerName]['leave'] - $raidData['player'][$playerName]['join'])/$totalTime*200);
            $raidData['player'][$playerName]['raidtime3'] = round(($raidData['end'] - $raidData['player'][$playerName]['leave'])/$totalTime*200);
        }

        foreach($data['raidinfo']['bosskills']['bosskill'] as $key=>$val) {
            $bossName   = trim($val['name']);
            $raidData['event'][$bossName]['time'] = ($val['time']);
            $raidData['event'][$bossName]['isboss'] = true;
            $raidData['event'][$bossName]['usernum']    = 0;
            $raidData['event'][$bossName]['boss']    = $bossName;
            $raidData['event'][$bossName]['costs']    = 0;      //没记录，在loot里计算
            if($bossName) $raidData['bossArray'][$bossName]  = $bossName;
            //如果记录了bosskill在场人数，则按当时在线人员。否则按时间
            foreach($raidData['player'] as $k=>$v) {
                if($v['join'] <= $val['time'] and $v['leave'] >= $val['time']) {
                    $raidData['event'][$bossName]['member'][]	= $k;
                    $raidData['event'][$bossName]['usernum']++;
                }
            }
            $raidData['event'][$bossName]['usernum']    = $raidData['event'][$bossName]['usernum']?$raidData['event'][$bossName]['usernum']:1;
    	}
        $bossLost   = array();
        foreach($data['raidinfo']['loots']['loot'] as $key=>$val) {
            $playerName = trim($val['player']);
            $bossName   = trim($val['boss']);
            $itemName   = trim($val['itemname']);
			if(strstr($val['itemid'], ':')) {
				$tmp	= explode(':', $val['itemid']);
				$itemid	= $tmp[0];
			}else {
				$itemid	= $val['itemid'];
			}
			$raidData['loot'][$key]['name']    = $itemName;
            $raidData['loot'][$key]['costs']    = $val['costs'];
            $raidData['loot'][$key]['time']    = $val['time'];
            $raidData['loot'][$key]['itemid']    = $itemid;
            $raidData['loot'][$key]['count']    = $val['count'];
            $raidData['loot'][$key]['player']    = $val['player'];
            $raidData['loot'][$key]['icon']    = $val['icon'];
            $raidData['loot'][$key]['color']    = $val['color'];           //8位的?
            $raidData['loot'][$key]['boss']    = $bossName;
            $raidData['totalItemdkp']    += $val['costs'];
            $bossLost[$bossName][]			= $raidData['loot'][$key];
            $raidData['event'][$bossName]['totalCosts']  += $val['costs'];
            $raidData['event'][$bossName]['usernum']    = $raidData['event'][$bossName]['usernum']?$raidData['event'][$bossName]['usernum']:1;
    	}
        //更新boss掉落装备
        foreach($raidData['event'] as $key=>$val) {
            if($val['isboss']) {
                $raidData['event'][$key]['item']  = $bossLost[$key];
            }
        }
    }elseif($data['midkp']) {                                                   //midkp多出$val['point']
        $raidData['start']  = strtotime(str_replace('.','-',$data['midkp']['raid']['start']));   //2009.06.05 18:48
        $raidData['end']  = strtotime(str_replace('.','-',$data['midkp']['raid']['end']));       //2009.06.05 18:48
        $raidData['zone']  = $data['midkp']['raid']['places']['place'];
        
    	foreach($data['midkp']['raid']['members']['member'] as $key=>$val) {
    		$playerName = trim($val['name']);
            $raidData['player'][$playerName]['class'] = ($val['class']);
            $raidData['player'][$playerName]['join']    = 0;
            $raidData['player'][$playerName]['leave']    = 0;
            $raidData['player'][$playerName]['raidtime1']   = 0;        //midkp没记录加入和退出时间
            $raidData['player'][$playerName]['raidtime2']   = 200;
            $raidData['player'][$playerName]['raidtime3']   = 0;
            $raidData['player'][$playerName]['index'] = $index++;
    	}
        foreach($data['midkp']['raid']['events']['event'] as $key=>$val) {
            $eventName   = trim($val['name']);
            $bossName   = trim($val['boss']);
            $raidData['event'][$eventName]['time'] = strtotime(str_replace('.','-',$val['time']));             //2009.06.05 19:32
            $raidData['event'][$eventName]['isboss'] = $bossName?true:false;
            if($bossName) $raidData['bossArray'][$eventName]  = $eventName;
            $raidData['event'][$eventName]['usernum']   = 0;
            $raidData['event'][$eventName]['boss']    = $bossName;
            $raidData['event'][$eventName]['costs']    = trim($val['point']);
            if(is_array($val['members']['member'])) {
            	foreach($val['members']['member'] as $k=>$v) {
                    $raidData['event'][$eventName]['member'][] = $v; 
                    $raidData['event'][$eventName]['usernum']++;
                }
            }else {
            	$raidData['event'][$eventName]['member'][] = $val['members']['member'];
                $raidData['event'][$eventName]['usernum']++;
            }
            $raidData['event'][$eventName]['totalCosts']  = 0;
            $raidData['event'][$eventName]['item']    = array();        //midkp不记录boss掉落
    	}
        foreach($data['midkp']['raid']['items']['item']  as $key=>$val) {
            $playerName = trim($val['looter']);
            $bossName   = trim($val['boss']);
            //$cffa335ee$Hitem:34195:0:0:0:0:0:0:1687957760:70$h[盛怒护肩]$h$r
            preg_match('/cff([0-f]{6})\$Hitem:([0-9]*)[^[]*\[([^]]*)/i', $val['name'], $match);
            $itemName   = trim($match[3]);
			$raidData['loot'][$key]['name']    = $itemName;
            $raidData['loot'][$key]['costs']    = $val['point'];
            $raidData['loot'][$key]['time']    = strtotime(str_replace('.','-',$val['time'])); 
            $raidData['loot'][$key]['itemid']    = $match[2];
            $raidData['loot'][$key]['count']    = 1;
            $raidData['loot'][$key]['player']    = $playerName;
            $raidData['loot'][$key]['icon']    = '';
            $raidData['loot'][$key]['color']    = $match[1];           //8位的?
            $raidData['loot'][$key]['boss']    = $bossName;
            $raidData['totalItemdkp']    += $val['point'];
    	}
        $raidData['nobossLoot']    = $raidData['loot'];        //midkp没有记录物品属于哪个boss掉落，所以都记为noboss
    }elseif($data['RaidInfo']['PlayerInfos']['key1']) {
    	//旧格式
		if(!preg_match("[0-9]+", $data['RaidInfo']['start'])) $data['RaidInfo']['start']	= strtotime($data['RaidInfo']['start']);
		if(!preg_match("[0-9]+",$data['RaidInfo']['end'])) $data['RaidInfo']['end']	= strtotime($data['RaidInfo']['end']);
		$raidData['start']  = $data['RaidInfo']['start'];   //int
        $raidData['end']  = $data['RaidInfo']['end'];       //int
        $raidData['zone']  = $data['RaidInfo']['zone'];
        $totalTime      = $raidData['end']-$raidData['start'];
        $totalTime      = $totalTime?$totalTime:1;

        foreach($data['RaidInfo']['PlayerInfos'] as $key=>$val) {
            $playerName = trim($val['name']);
            $raidData['player'][$playerName]['race'] = ($val['race']);
            $raidData['player'][$playerName]['class'] = ($val['class']);
            $raidData['player'][$playerName]['level'] = ($val['level']);
            $raidData['player'][$playerName]['index'] = $index++;
        }
		
        foreach($data['RaidInfo']['Join'] as $key=>$val) {
            $playerName = trim($val['player']);
			if(!preg_match("[0-9]+",$val['time'])) $val['time']	= strtotime($val['time']);
            $raidData['player'][$playerName]['join']    = $val['time'];
    	}
        foreach($data['RaidInfo']['Leave'] as $key=>$val) {
            $playerName = trim($val['player']);
			if(!preg_match("[0-9]+",$val['time'])) $val['time']	= strtotime($val['time']);
            $raidData['player'][$playerName]['leave']    = $val['time'];
    	}
		
        foreach($raidData['player'] as $key=>$val) {
			$playerName		= $key;
        	$raidData['player'][$playerName]['raidtime1'] = round(($raidData['player'][$playerName]['join'] - $raidData['start'])/$totalTime*200);
            $raidData['player'][$playerName]['raidtime2'] = round(($raidData['player'][$playerName]['leave'] - $raidData['player'][$playerName]['join'])/$totalTime*200);
            $raidData['player'][$playerName]['raidtime3'] = round(($raidData['end'] - $raidData['player'][$playerName]['leave'])/$totalTime*200);
        }

        foreach($data['RaidInfo']['BossKills'] as $key=>$val) {
            $bossName   = trim($val['name']);
			if(!preg_match("[0-9]+",$val['time'])) $val['time']	= strtotime($val['time']);
            $raidData['event'][$bossName]['time'] = $val['time'];
            $raidData['event'][$bossName]['isboss'] = true;
            //$raidData['event'][$bossName]['usernum']    = 0;
            $raidData['event'][$bossName]['boss']    = $bossName;
            $raidData['event'][$bossName]['costs']    = 0;      //没记录，在loot里计算
            if($bossName) $raidData['bossArray'][$bossName]  = $bossName;
            //如果记录了bosskill在场人数，则按当时在线人员。否则按时间
			if(is_array($val['attendees']['at_player'])) {
				foreach($val['attendees']['at_player'] as $k=>$v) {
					$raidData['event'][$bossName]['member'][]	= $v['at_name'];
					$raidData['event'][$bossName]['usernum']++;
				}
			}else {
				foreach($raidData['player'] as $k=>$v) {
					if($v['join'] <= $val['time'] and $v['leave'] >= $val['time']) {
						$raidData['event'][$bossName]['member'][]	= $k;
						$raidData['event'][$bossName]['usernum']++;
					}
				}
			}
            $raidData['event'][$bossName]['usernum']    = $raidData['event'][$bossName]['usernum']?$raidData['event'][$bossName]['usernum']:1;
    	}
		
        $bossLost   = array();
		$i			= 0;
        foreach($data['RaidInfo']['Loot'] as $key=>$val) {
            $playerName = trim($val['Player']);
            $bossName   = trim($val['Boss']);
            $itemName   = trim($val['ItemName']);
			if(strstr($val['ItemID'], ':')) {
				$tmp	= explode(':', $val['itemid']);
				$itemid	= $tmp[0];
			}else {
				$itemid	= $val['ItemID'];
			}
			$raidData['loot'][$i]['name']    = $itemName;
            $raidData['loot'][$i]['costs']    = (float)$val['Costs'];
            $raidData['loot'][$i]['time']    = $val['Time'];
            $raidData['loot'][$i]['itemid']    = $itemid;
            $raidData['loot'][$i]['count']    = $val['Count'];
            $raidData['loot'][$i]['player']    = $playerName;
            $raidData['loot'][$i]['icon']    = $val['Icon'];
            $raidData['loot'][$i]['color']    = $val['Color'];           //8位的?
            $raidData['loot'][$i]['boss']    = $bossName;
            $raidData['totalItemdkp']    += $val['Costs'];
            $bossLost[$bossName][]			= $raidData['loot'][$i];
            $raidData['event'][$bossName]['totalCosts']  += $val['Costs'];
            $raidData['event'][$bossName]['usernum']    = $raidData['event'][$bossName]['usernum']?$raidData['event'][$bossName]['usernum']:1;
			$i++;
    	}
        //更新boss掉落装备
        foreach($raidData['event'] as $key=>$val) {
            if($val['isboss']) {
                $raidData['event'][$key]['item']  = $bossLost[$key];
            }
        }
    }
    else {
    	exit('Format not found.');
    }
    $raidData['totalTime']  = $raidData['end']>$raidData['start']?$raidData['end']-$raidData['start']:0;
    $raidData['totalMember']    = count($raidData['player']);

    Return $raidData;
}

//会员 boss 装备 

function GetXMLTree ($xmldata)
{
    // we want to know if an error occurs
    ini_set ('track_errors', '1');

    $xmlreaderror = false;
    //2006-9-11 attributes不初始化会在某些php版本中不可用
    $attributes		= array();

    $parser = xml_parser_create ('UTF-8');
    xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, 0);
    if (!xml_parse_into_struct ($parser, $xmldata, $vals, $index)) {
        $xmlreaderror = true;
        echo "error log.";
        exit;
    }
    xml_parser_free ($parser);

    if (!$xmlreaderror) {
        $result = array ();
        $i = 0;
        if (isset ($vals[$i]['attributes']))
            foreach (array_keys ($vals[$i]['attributes']) as $attkey)
            $attributes[$attkey] = $vals[$i]['attributes'][$attkey];
        $result [$vals [$i]['tag']] = array_merge ($attributes, GetChildren ($vals, $i, 'open'));

    }
    ini_set ('track_errors', '0');
    return $result;
}

function GetChildren ($vals, &$i, $type)
{
    if ($type == 'complete') {
        if (isset ($vals [$i]['value']))
            return ($vals [$i]['value']);
        else
            return '';
    }

    $children = array (); // Contains node data

    /* Loop through children */
    while ($vals [++$i]['type'] != 'close') {
        $type = $vals [$i]['type'];
        // first check if we already have one and need to create an array
        if (isset ($children [$vals [$i]['tag']])) {
            if (is_array ($children [$vals [$i]['tag']])) {
                $temp = array_keys ($children [$vals [$i]['tag']]);
                // there is one of these things already and it is itself an array
                if (is_string ($temp [0])) {
                    $a = $children [$vals [$i]['tag']];
                    unset ($children [$vals [$i]['tag']]);
                    $children [$vals [$i]['tag']][0] = $a;
                }
            } else {
                $a = $children [$vals [$i]['tag']];
                unset ($children [$vals [$i]['tag']]);
                $children [$vals [$i]['tag']][0] = $a;
            }

            $children [$vals [$i]['tag']][] = GetChildren ($vals, $i, $type);
        } else
            $children [$vals [$i]['tag']] = GetChildren ($vals, $i, $type);
        // I don't think I need attributes but this is how I would do them:
        if (isset ($vals [$i]['attributes'])) {
            $attributes = array ();
            foreach (array_keys ($vals [$i]['attributes']) as $attkey)
            $attributes [$attkey] = $vals [$i]['attributes'][$attkey];
            // now check: do we already have an array or a value?
            if (isset ($children [$vals [$i]['tag']])) {
                // case where there is an attribute but no value, a complete with an attribute in other words
                if ($children [$vals [$i]['tag']] == '') {
                    unset ($children [$vals [$i]['tag']]);
                    $children [$vals [$i]['tag']] = $attributes;
                }
                // case where there is an array of identical items with attributes
                elseif (is_array ($children [$vals [$i]['tag']])) {
                    $index = count ($children [$vals [$i]['tag']]) - 1;
                    // probably also have to check here whether the individual item is also an array or not or what... all a bit messy
                    if ($children [$vals [$i]['tag']][$index] == '') {
                        unset ($children [$vals [$i]['tag']][$index]);
                        $children [$vals [$i]['tag']][$index] = $attributes;
                    }
                    $children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes);
                } else {
                    $value = $children [$vals [$i]['tag']];
                    unset ($children [$vals [$i]['tag']]);
                    $children [$vals [$i]['tag']]['value'] = $value;
                    $children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);
                }
            } else
                $children [$vals [$i]['tag']] = $attributes;
        }
    }
    return $children;
}

/**
 * 判断物品品质
 */
function isValidItem($itemcolor,$itemcfg){
    //$GLOBALS['itemquality']
    $itemcolor	= strtolower($itemcolor);
    switch ($itemcolor){
        case 'e6cc80':
            $v	= 0;
        break;
        case 'ff8000':
            $v	= 1;
        break;
        case 'a335ee':
            $v	= 2;
        break;
        case '0070dd':
            $v	= 3;
        break;
        case '1eff00':
            $v	= 4;
        break;
        case 'ffffff':
            $v	= 5;
        break;
        case '9d9d9d':
            $v	= 6;
        break;
        default:
            $v	= 4;
    }
    if ($v <= $itemcfg) {
        return true;
    }else {
        return false;
    }
}

?>